package powell.nflpredictionproject.display;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 *
 * @author Joshua Powell
 */

/*
 * This class is the JFrame for the UserPanel class and extends the JFrame class
 */
public class MainPanel extends JFrame
{

    /*
     * This is the component and variable declarations on the class level.
     */
    private ImageIcon image = new ImageIcon("NFLTEAMS.JPG");
    private UserPanel user = new UserPanel();
    private static int answer = 0;
    private JMenuBar bar = new JMenuBar();
    private JMenu menu = new JMenu("Choose A Year Here");
    private JRadioButton twoOTwelve = new JRadioButton("2012");
    private JRadioButton twoOThirteen = new JRadioButton("2013");
    private ButtonGroup group = new ButtonGroup();
    private JLabel pictureLabel = new JLabel(image);
    private JLabel statusBar = new JLabel();
    private JLabel spaceTakerLabel = new JLabel("           ");

    /*
     * This is the constructor for the MainPanel class.
     */
    public MainPanel()
    {
        menu.setToolTipText("ALT-Y to open the menu to choose a year");
        menu.setMnemonic(KeyEvent.VK_Y);

        twoOTwelve.setToolTipText("ALT-2 to select the year 2012");
        twoOTwelve.setMnemonic(KeyEvent.VK_2);

        twoOThirteen.setToolTipText("ALT-3 to select the year 2013");
        twoOThirteen.setMnemonic(KeyEvent.VK_3);

        this.setBounds(500, 100, 1700, 1700);

        this.setResizable(false);

        statusBar.setForeground(Color.BLUE);
        statusBar.setText("USING 2012 STATS");

        answer = 0;

        group.add(twoOTwelve);
        group.add(twoOThirteen);

        twoOTwelve.addActionListener(new ButtonListener());
        twoOThirteen.addActionListener(new ButtonListener());

        menu.add(twoOTwelve);

        menu.add(twoOThirteen);

        bar.setBackground(Color.WHITE);

        bar.setForeground(Color.BLACK);

        bar.add(menu);
        bar.add(spaceTakerLabel);
        bar.add(statusBar);

        this.add(bar);

        setJMenuBar(bar);

        this.setTitle("NFL Prediction Application");

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        user.getCalculateOne().addActionListener(new CalculateListener());
        user.getCalculateFive().addActionListener(new CalculateListener());

        this.setLayout(new BorderLayout());

        this.add(pictureLabel, BorderLayout.NORTH);
        this.add(user, BorderLayout.SOUTH);


        this.pack();

        this.setVisible(true);
    }

    /*
     * This method closes the MainPanel by making the .setVisible() property to
     * false and then calling the .dispose() method of this to get rid of the
     * MainPanel JFrame.
     */
    private void closeMe()
    {
        this.setVisible(false);
        this.dispose();
    }

    /*
     * This method gets answer and returns answer.
     * 
     * @return answer
     */
    public static int getAnswer()
    {
        return answer;
    }

    /*
     * This class implements the ActionListener interface and uses the
     * actionPerformed() method along with if statements 
     * to check to see what JRadioButton is selected and then changes answer to 
     * either 0 or 1. The actionPerformed() method also calls the 
     * .setForeground() and .setText() properties of the statusBar to change it
     * to the appropriate values.
     */
    private class ButtonListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (twoOTwelve == e.getSource())
            {
                answer = 0;
                statusBar.setForeground(Color.BLUE);
                statusBar.setText("USING 2012 STATS");
            }

            if (twoOThirteen == e.getSource())
            {
                answer = 1;
                statusBar.setForeground(Color.RED);
                statusBar.setText("USING 2013 STATS");
            }
        }
    }

    /*
     * This class implements the ActionListener interface and uses the
     * actionPerformed() method to call the closeMe() method if the calculateOne
     * or calculateFive JButton is clicked.
     */
    private class CalculateListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            closeMe();
        }
    }
}
